package edu.vt.sdm.tcga.filenavigator.model;

import java.util.Iterator;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import edu.vt.sdm.tcga.filenavigator.parser.TCGAFileParser;
import edu.vt.sdm.tcga.filenavigator.view.LabelContentProvider;
import edu.vt.sdm.tcga.filenavigator.view.TreeContentProvider;

public class FileSystemNavigatorView extends ViewPart{

	protected TreeViewer treeViewer;
	protected TreeContentProvider contentProvider;
	protected LabelContentProvider labelProvider;
	protected Action actionMenu;
	private String selectedFile;

	protected Object getInitialInput() {
		return new NavigatorRoot();
	}

	@Override
	public void createPartControl(Composite parent) {

		contentProvider = new TreeContentProvider();
		labelProvider = new LabelContentProvider();

		treeViewer = new TreeViewer(parent);
		treeViewer.setContentProvider(contentProvider);
		treeViewer.setLabelProvider(labelProvider);

		treeViewer.setUseHashlookup(true);
		treeViewer.setInput(getInitialInput());

		makeActions();
		hookContextMenu();
		contributeToActionBars();
		hookListeners();
	}

	@Override
	public void setFocus() {
	}

	protected void hookListeners() {

		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@SuppressWarnings("unchecked")
			public void selectionChanged(SelectionChangedEvent event) {
				// if the selection is empty clear the label
				if (event.getSelection().isEmpty()) {
					selectedFile = "";
					return;
				}
				if (event.getSelection() instanceof IStructuredSelection) {
					IStructuredSelection selection = (IStructuredSelection) event
							.getSelection();

					for (Iterator iterator = selection.iterator(); iterator
							.hasNext();) {
						Object domain = (FileBean) iterator.next();
						selectedFile = ((FileBean) domain).getFullFilePath();
					}
				}
			}
		});
	}
	
	private void makeActions(){
		
		actionMenu = new Action(){
			public void run(){
				
				showMessage("Preparing to parse genome file: " + selectedFile );

				String[] args = new String[1];
				args[0] = selectedFile;
				TCGAFileParser.parse(args);

				showMessage("Done parsing genome file: " + selectedFile );
			}
		};
		
		actionMenu.setText("Parse Genome Data File");
		actionMenu.setToolTipText("Load Genome data file");
		actionMenu.setImageDescriptor(PlatformUI
				.getWorkbench()
				.getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_OBJ_ELEMENT));
	}
	
	private void showMessage(String message){
		MessageDialog.openInformation(treeViewer
				.getControl()
				.getShell(), 
				"File System Navigator", 
				message);
	}
	
	private void hookContextMenu(){
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener(){
			public void menuAboutToShow(IMenuManager manager) {
				FileSystemNavigatorView.this.fillContextMenu(manager);
			}
			
		});
		
		Menu menu = menuMgr.createContextMenu(treeViewer.getControl());
		treeViewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, treeViewer);
	}

	private void contributeToActionBars(){
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPulldown(bars.getMenuManager());
		fillLocalToolbar(bars.getToolBarManager());
	}
	
	private void fillContextMenu(IMenuManager manager){
		manager.add(actionMenu);
		manager.add(new Separator());
		
		// Other plugins can contribute their actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}

	private void fillLocalToolbar(IToolBarManager manager){
		manager.add(actionMenu);
		manager.add(new Separator());
	}
	
	private void fillLocalPulldown(IMenuManager manager){
		manager.add(actionMenu);
		manager.add(new Separator());
	}

}
